// bzoj2258
//
// my first algorithm TLE.
// every time brute force calc sa. O(I * nlog(n) + Q)
//
// run: $exec < input
// opt: 0
// flag: -g
//

#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>

template <
	class T,
	class COMPARE = std::less<T>
>
struct sparse_table
{
	typedef T value_type;
	typedef unsigned int size_type;
	typedef COMPARE comparer;
	typedef std::vector<value_type> vector_type;

	inline size_type log2(size_type len) const
	{
		size_type tmp = 0;
		for (; size_type(1 << tmp) <= len; tmp++); // FIXME 11 neednot write size_type
		return tmp - 1;
	}

	sparse_table(comparer cmp = comparer()) : cmp(cmp) {}

	void init(vector_type const & data)
	{
		size_type n = data.size();
		size_type len = log2(n);

		for (size_type i = 0; i < n; i++) st[i][0] = i;
		for (size_type l = 1; l <= len; l++)
			for (size_type i = 0; (i + (1<<l) - 1) < n; i++)
				if (cmp(data[ st[i][l - 1] ], data[ st[i + (1<<(l-1))][l - 1] ]))
					st[i][l] = st[i][l - 1];
				else
					st[i][l] = st[i + (1 << (l-1))][l - 1];
	}

	// [l, r]
	size_type query_interval_index(vector_type const & data, size_type l, size_type r) const
	{
		int k = log2(r - l + 1);
		if (cmp(data[ st[l][k] ], data[ st[r - (1<<k) + 1][k] ]))
			return st[l][k];
		else
			return st[r - (1<<k) + 1][k];
	}

	// [l, r]
	value_type query_interval(vector_type const & data, size_type l, size_type r) const
	{
		return data[ query_interval_index(data, l, r) ];
	}

private:
	size_type static const maxn = 100007;
	size_type static const  len = 17;
	size_type st[maxn][len];
	comparer cmp;
};

struct suffix_array
{
	typedef std::vector<int> & v_ref;
	typedef std::vector<int> const & v_cref;

	struct suffix_cmp
	{
		suffix_cmp(v_cref rank, int const & gap, int const & len)
			: rank(rank), gap(gap), len(len) {}

		bool operator()(int i, int j)
		{
			if (rank[i] != rank[j]) return rank[i] < rank[j];
			return (i + gap < len && j + gap < len) ?
						(rank[i + gap] < rank[j + gap]) : i > j;
		}

	private:
		v_cref rank;
		int const & gap;
		int const & len;
	};

	suffix_array(v_cref s, int n, v_ref sa, v_ref rank, v_ref height)
		: s(s), n(n), sa(sa), rank(rank), height(height)
	{
		tmp_rank.resize(2 * n + 100);
	}

	void resize(int n)
	{
		this->n = n;
	}

	void build_suffix_array()
	{
		for (int i = 0; i < n; i++) { sa[i] = i; rank[i] = s[i]; }
		for (int gap = 1; ; gap *= 2) {
			suffix_cmp cmp(rank, gap, n);
			std::sort(sa.begin(), sa.begin() + n, cmp);
			for (int i = 0; i < n-1; i++)
				tmp_rank[i + 1] = tmp_rank[i] + cmp(sa[i], sa[i + 1]);
			for (int i = 0; i < n; i++) rank[sa[i]] = tmp_rank[i];
			if (tmp_rank[n - 1] == n - 1) break;
		}
	}

	void build_lcp()
	{
		for (int i = 0, k = 0; i < n; i++)
			if (rank[i] != n-1) {
				for (int j = sa[rank[i] + 1]; s[i + k] == s[j + k]; k++);
				height[rank[i]] = k;
				if (k) k--;
			}
	}

private:
	v_cref s;
	int n;
	v_ref sa; // suffix array
	v_ref rank;
	v_ref height; // lcp array, longest common prefix array
	std::vector<int> tmp_rank;
};

int const maxn = 50007;

std::vector<int> inserted;

int get_new_index(int x)
{
	for (int i = 0; i < (int)inserted.size(); i++)
		if (inserted[i] <= x) x++;
	return x;
}

int main()
{
	char s[maxn];
	std::scanf("%s", s);
	int n = std::strlen(s);
	std::vector<int> da(n);
	for (int i = 0; i < n; i++) da[i] = s[i];

	std::vector<int> sa(n);
	std::vector<int> rank(n);
	std::vector<int> height(n);
	suffix_array suffix(da, n, sa, rank, height);

	int m;
	std::scanf("%d", &m);
	bool build = false;
	sparse_table<int> st;
	for (int i = 0; i < m; i++) {
		char ch;
		std::scanf("\n%c", &ch);
		if (ch == 'I') {
			int p;
			std::scanf(" %c %d", &ch, &p);
			inserted.push_back(--p);
			suffix.resize(++n);
			da.insert(da.begin() + p, ch);
			sa.push_back(0);
			rank.push_back(0);
			height.push_back(0);
			build = false;
		} else {
			int x, y;
			std::scanf("%d %d", &x, &y);
			x--; y--;
			if (!build) {
				suffix.build_suffix_array();
				suffix.build_lcp();
				st.init(height);
				build = true;
			}
			int px = get_new_index(x);
			int py = get_new_index(y);
			int l = rank[px], r = rank[py];
			if (r < l) std::swap(l, r);
			if (l == r) std::printf("%d\n", n - px);
			else std::printf("%d\n", st.query_interval(height, l, r - 1));
		}
	}
}

